@use 'sass:map';

@use './shared' as *;
@use './design' as *;

$input: () !default;
$input: map.merge(
  (
    bg-color: get-css-var('fill-color-base'),
    bg-color-disabled: get-css-var('fill-color-humble'),
    b-color: get-css-var('border-color-base'),
    b-color-hover: get-css-var('color-primary-light-1'),
    b-color-focus: get-css-var('input-b-color-hover'),
    s-color-focus: get-css-var('color-primary-opacity-6'),
    placeholder-color: get-css-var('content-color-placeholder'),
    control-color: get-css-var('content-color-base'),
    control-color-disabled: get-css-var('content-color-disabled'),
    icon-color: get-css-var('content-color-secondary'),
    icon-color-hover: get-css-var('color-primary-base'),
    icon-color-disabled: get-css-var('input-control-color-disabled'),
    before-bg-color: get-css-var('fill-color-background'),
    after-bg-color: get-css-var('fill-color-background'),
    line-height: get-css-var('line-height-base'),
    border: get-css-var('border-shape') get-css-var('input-b-color'),
    radius: get-css-var('radius-base'),
    count-color: get-css-var('content-color-secondary'),
    count-color-disabled: get-css-var('input-control-color-disabled'),
    count-font-size: get-css-var('font-size-secondary'),
    count-line-height: get-css-var('line-height-secondary'),
    height: 32px,
    v-padding: 4px,
    h-padding: 8px,
    padding: get-css-var('input-v-padding') get-css-var('input-h-padding'),
    prefix-width: 32px,
    suffix-width: 32px
  ),
  $input
);

@mixin define-input-style($style-map) {
  @include define-preset-style('input', $style-map);
}

@mixin handler($handler: &) {
  @at-root #{$handler} {
    position: relative;
    display: inline-flex;
    align-items: center;
    width: 100%;
    height: get-css-var('input-height');
    padding: get-css-var('input-v-padding') get-css-var('input-h-padding');
    cursor: text;
    background-color: get-css-var('input-bg-color');
    border: get-css-var('border-shape') get-css-var('input-b-color');
    border-radius: get-css-var('input-radius');
    outline: 0;
    transition: get-css-var('transition-background'), get-css-var('transition-border'),
      get-css-var('transition-shadow');

    &:hover {
      border-color: get-css-var('input-b-color-hover');
    }

    &:focus,
    &:focus-within,
    &--focused {
      z-index: 2;
      border-color: get-css-var('input-b-color-focus');
      box-shadow: get-css-var('shadow-focus') get-css-var('input-s-color-focus');
    }

    &--small {
      @include define-preset-values(
        'input',
        (
          height: 24px,
          v-padding: 1px
        )
      );
    }

    &--large {
      @include define-preset-values(
        'input',
        (
          height: 40px,
          v-padding: 7px
        )
      );
    }

    $states: success, error, warning;

    @each $state in $states {
      // 提升 state 样式的级别，以防止引入的先后顺序导致样式丢失
      &#{&}--#{$state} {
        @include define-input-style(
          (
            b-color: 'color' $state 'light-1',
            b-color-hover: 'color' $state 'light-1',
            s-color-focus: 'color' $state 'opacity-6'
          )
        );
      }
    }

    &--loading {
      cursor: auto;
    }

    &--transparent {
      background-color: transparent;

      &,
      &:hover,
      &:focus,
      &:focus-within,
      &--focused {
        border-color: transparent;
        box-shadow: unset;
      }
    }

    &--disabled {
      cursor: not-allowed;
      background-color: get-css-var('input-bg-color-disabled');

      &,
      &:hover,
      &:focus,
      &:focus-within {
        border-color: get-css-var('border-color-base');
        box-shadow: none;
      }

      %disabled-item {
        color: get-css-var('input-control-color-disabled');
        pointer-events: none;
        cursor: not-allowed;
      }
    }

    &--disabled#{&}--focused {
      border-color: get-css-var('border-color-base');
      box-shadow: none;
    }
  }

  &__control {
    flex: 1 0 0;
    min-width: 0;
    height: 100%;
    padding: 0;
    font-size: inherit;
    line-height: get-css-var('input-line-height');
    color: get-css-var('input-control-color');
    background-color: transparent;
    border: 0;
    border-radius: get-css-var('input-radius');
    outline: 0;
    transition: get-css-var('transition-color');

    &:disabled {
      cursor: not-allowed;
    }

    &::placeholder {
      color: get-css-var('input-placeholder-color');
    }

    /* stylelint-disable no-duplicate-selectors */
    &::-ms-clear,
    &::-ms-clear,
    &::-ms-reveal,
    &::-ms-reveal {
      display: none;
    }
    /* stylelint-enable no-duplicate-selectors */

    @extend %disabled-item;
  }

  #{$handler}--loading &__control {
    cursor: auto;
  }

  &__icon {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: get-css-var('input-icon-color');
    user-select: none;

    &--placeholder {
      min-width: 14px;
      min-height: 1px;
      margin-inline-start: 4px;
      visibility: hidden;
      opacity: 0%;
    }

    @extend %disabled-item;
  }

  &__prefix {
    margin-inline-end: 4px;
  }

  &__suffix {
    margin-inline-start: 4px;
    transition: get-css-var('transition-opacity');
  }

  &__clear,
  &__loading {
    position: absolute;
    inset-inline-end: get-css-var('input-h-padding');
    min-width: 14px;
    margin-inline-start: 4px;
    cursor: pointer;
  }

  &__loading {
    pointer-events: none;
  }
}

.#{$namespace}-input {
  @at-root &-vars {
    @include define-preset-values('input', $input);
  }

  $self: &;
  $wrapper: #{&}-wrapper;
  $radius: get-css-var('input-radius');

  &-wrapper {
    @include basis;

    position: relative;
    display: inline-flex;
    align-items: center;
    width: 100%;
    background-color: get-css-var('input-bg-color');
  }

  @include basis;
  @include handler;

  &-wrapper & {
    flex: 1 0 0;
    width: auto;
    background-color: transparent;
    border-radius: 0;
  }

  &-wrapper--before-only & {
    @include logical-radius(0 $radius $radius 0);
  }

  &-wrapper--after-only & {
    @include logical-radius($radius 0 0 $radius);
  }

  &--before {
    z-index: 1;
    border-inline-start-color: transparent;
  }

  &--after {
    z-index: 1;
    border-inline-end-color: transparent;
  }

  &__suffix-wrapper {
    position: relative;
    display: flex;
    align-items: center;
  }

  &__clear,
  &__loading {
    inset-inline-end: 0;
  }

  &__password {
    margin-inline-start: 4px;
    cursor: pointer;
  }

  &__count {
    margin-inline-end: 4px;
    font-size: get-css-var('input-count-font-size');
    line-height: get-css-var('input-count-line-height');
    color: get-css-var('input-count-color');
    pointer-events: none;
    user-select: none;
    transition: get-css-var('transition-color');
  }

  &--disabled &__count {
    color: get-css-var('input-count-color-disabled');
  }

  &__before,
  &__after {
    position: relative;
    display: flex;
    align-items: center;
    height: calc(get-css-var('input-height'));
    padding: 0 get-css-var('input-h-padding');
    border: get-css-var('input-border');

    &--action {
      padding: 0;
      border: 0;
    }
  }

  @mixin inner-input($inner-radius) {
    & > #{$wrapper},
    .#{$namespace}-number-input {
      width: calc(100% + 2 * get-css-var('input-h-padding'));
      height: 100%;

      @include logical-radius($inner-radius);
    }
  }

  @mixin inner-select($inner-radius) {
    .#{$namespace}-cascader,
    .#{$namespace}-color-picker,
    .#{$namespace}-date-picker,
    .#{$namespace}-select,
    .#{$namespace}-time-picker {
      width: calc(100% + 2 * get-css-var('input-h-padding'));
      height: 100%;

      &__selector {
        min-width: 60px;
        height: 100%;
        background-color: transparent;

        @include logical-radius($inner-radius);

        box-shadow: unset;
      }
    }
  }

  @mixin inner-button($inner-radius) {
    .#{$namespace}-button {
      width: calc(100% + 2 * get-css-var('input-h-padding'));
      height: 100%;

      @include logical-radius($inner-radius);

      &--icon-only {
        width: calc(get-css-var('button-height') + 2 * get-css-var('input-h-padding'));
      }
    }
  }

  $before-radius: $radius 0 0 $radius;
  $after-radius: 0 $radius $radius 0;

  &__before {
    $bg-color: get-css-var('input-before-bg-color');

    background-color: $bg-color;
    border-inline-end: 0;

    @include logical-radius($radius 0 0 $radius);

    &--action {
      margin-inline-end: -1px;
      background-color: transparent;

      @include inner-button($before-radius);
      @include inner-input($before-radius);
      @include inner-select($before-radius);
    }
  }

  &__after {
    $bg-color: get-css-var('input-after-bg-color');

    background-color: $bg-color;
    border-inline-start: 0;

    @include logical-radius(0 $radius $radius 0);

    &--action {
      margin-inline-start: -1px;
      background-color: transparent;

      @include inner-button($after-radius);
      @include inner-input($after-radius);
      @include inner-select($after-radius);
    }
  }

  &--transparent &__before,
  &--transparent &__after {
    background-color: transparent;
  }
}
